package com.common.service.impl;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.mapper.GeneratorMapper;
import com.common.service.GeneratorService;
import com.common.utils.GenUtils;


@Service
public class GeneratorServiceImpl implements GeneratorService {
  @Autowired
  GeneratorMapper generatorMapper;

  @Override
  public List<Map<String, Object>> list() {
    List<Map<String, Object>> list = generatorMapper.list();
    return list;
  }

  @Override
  public byte[] generatorCode(String[] tableNames) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(outputStream);
    for (String tableName : tableNames) {
      //查询表信息
      Map<String, String> table = generatorMapper.get(tableName);
      //查询列信息
      List<Map<String, String>> columns = generatorMapper.listColumns(tableName);
      //生成代码
      try {
        GenUtils.generatorCode(table, columns, zip);
      } catch (ResourceNotFoundException e) {
        e.printStackTrace();
      } catch (ParseErrorException e) {
        e.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    IOUtils.closeQuietly(zip);
    return outputStream.toByteArray();
  }

}
